package com.ruoyi.blogs.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Date;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.blogs.domain.BsGroup;
import com.ruoyi.blogs.mapper.BsDirectionMapper;
import com.ruoyi.blogs.domain.BsDirection;
import com.ruoyi.blogs.service.IBsDirectionService;

/**
 * 方向Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-11-09
 */
@Service
public class BsDirectionServiceImpl implements IBsDirectionService 
{
    @Autowired
    private BsDirectionMapper bsDirectionMapper;

    /**
     * 查询方向
     * 
     * @param id 方向主键
     * @return 方向
     */
    @Override
    public BsDirection selectBsDirectionById(Long id)
    {
        return bsDirectionMapper.selectBsDirectionById(id);
    }

    /**
     * 查询方向列表
     * 
     * @param bsDirection 方向
     * @return 方向
     */
    @Override
    public List<BsDirection> selectBsDirectionList(BsDirection bsDirection)
    {
        return bsDirectionMapper.selectBsDirectionList(bsDirection);
    }

    /**
     * 新增方向
     * 
     * @param bsDirection 方向
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBsDirection(BsDirection bsDirection)
    {
        bsDirection.setCreateTime(DateUtils.getNowDate());
        bsDirection.setUpdateTime(DateUtils.getNowDate());
        int rows = bsDirectionMapper.insertBsDirection(bsDirection);
        insertBsGroup(bsDirection);
        return rows;
    }

    /**
     * 修改方向
     * 
     * @param bsDirection 方向
     * @return 结果
     */
    @Transactional
    @Override
    public int updateBsDirection(BsDirection bsDirection)
    {
        bsDirection.setUpdateTime(DateUtils.getNowDate());
        bsDirectionMapper.deleteBsGroupByDirectionId(bsDirection.getId());
        insertBsGroup(bsDirection);
        return bsDirectionMapper.updateBsDirection(bsDirection);
    }

    /**
     * 批量删除方向
     * 
     * @param ids 需要删除的方向主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBsDirectionByIds(Long[] ids)
    {
        bsDirectionMapper.deleteBsGroupByDirectionIds(ids);
        return bsDirectionMapper.deleteBsDirectionByIds(ids);
    }

    /**
     * 删除方向信息
     * 
     * @param id 方向主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteBsDirectionById(Long id)
    {
        bsDirectionMapper.deleteBsGroupByDirectionId(id);
        return bsDirectionMapper.deleteBsDirectionById(id);
    }

    /**
     * 新增分类信息
     * 
     * @param bsDirection 方向对象
     */
    public void insertBsGroup(BsDirection bsDirection)
    {
        Date date = DateUtils.getNowDate();
        List<BsGroup> bsGroupList = bsDirection.getBsGroupList();
        Long id = bsDirection.getId();
        if (StringUtils.isNotNull(bsGroupList))
        {
            List<BsGroup> list = new ArrayList<BsGroup>();
            for (BsGroup bsGroup : bsGroupList)
            {
                bsGroup.setUpdateTime(date);
                bsGroup.setDirectionId(id);
                list.add(bsGroup);
            }
            if (list.size() > 0)
            {
                bsDirectionMapper.batchBsGroup(list);
            }
        }
    }
}
